<?php

namespace app\common\model\xilujob;

use think\Model;
use think\model\Pivot;
use function fast\array_get;

class EnterpriseBenefit extends Pivot
{
    //
    protected $name = 'xilujob_enterprise_benefit';

    public static function save_info($enterprise_info,$benefit_ids){

        if($benefit_ids){
            $insertBenefits = [];
            $benefit_ids = array_get($benefit_ids,'benefit_ids');
            $benefitIds = is_array($benefit_ids) ? $benefit_ids : explode(',', $benefit_ids);
            \app\admin\model\xilujob\EnterpriseBenefit::whereNotIn('benefit_id', $benefitIds)->where('enterprise_id', $enterprise_info->id)->delete();
            foreach ($benefitIds as &$benefitId) {
                if(EnterpriseBenefit::where('enterprise_id', $enterprise_info->id)->where('benefit_id',$benefitId)->count()<1)
                    $insertBenefits[] = ['enterprise_id'=>$enterprise_info->id, 'benefit_id'=>$benefitId];
            }
            if($insertBenefits) {
                $cb = new EnterpriseBenefit();
                $cb->saveAll($insertBenefits);
            }
        }else {
            EnterpriseBenefit::where('enterprise_id', $enterprise_info->id)->delete();
        }


    }
}
